package com.eclink.asset.workflow.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eclink.asset.asset.domain.dao.FlowApply;
import com.eclink.asset.common.exception.BusinessException;
import com.eclink.asset.setup.domain.dao.Company;
import com.eclink.asset.sys.domain.dto.SysUserDetailDTO;
import com.eclink.asset.sys.service.SysUserService;
import com.eclink.asset.workflow.domain.dao.FlowTask;
import com.eclink.asset.workflow.domain.dao.Todo;
import com.eclink.asset.workflow.domain.dto.DoTaskDTO;
import com.eclink.asset.workflow.domain.dto.TodoInfoDTO;
import com.eclink.asset.workflow.enums.TodoTypeEnum;
import com.eclink.asset.workflow.mapper.TodoMapper;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
public class TodoService {

    @Resource
    private TodoMapper todoMapper;

    @Resource
    private FlowService flowService;

    @Resource
    private SysUserService sysUserService;

    /**
     * 查询所有待办数量
     * @return 数量
     */
    public Integer getTodoCountAll() {
        LambdaQueryWrapper<Todo> qw = new LambdaQueryWrapper<>();
        qw.eq(Todo::getUserId, SecurityUtils.getUserId());
        qw.eq(Todo::isFinish, false);
        return todoMapper.selectCount(qw);
    }

    /**
     * 查询审批任务数量
     * @return 数量
     */
    public Integer getTodoCountApprove() {
        LambdaQueryWrapper<Todo> qw = new LambdaQueryWrapper<>();
        qw.eq(Todo::getTodoType, TodoTypeEnum.APPROVE.getValue());
        qw.eq(Todo::getUserId, SecurityUtils.getUserId());
        qw.eq(Todo::isFinish, false);
        return todoMapper.selectCount(qw);
    }

    /**
     * 查询用户申请数量
     * @return 数量
     */
    public Integer getTodoCountEmployee() {
        LambdaQueryWrapper<Todo> qw = new LambdaQueryWrapper<>();
        qw.eq(Todo::getTodoType, TodoTypeEnum.EMPLOYEE.getValue());
        qw.eq(Todo::getUserId, SecurityUtils.getUserId());
        qw.eq(Todo::isFinish, false);
        qw.orderByDesc(Todo::getCreateTime);
        return todoMapper.selectCount(qw);
    }

    /**
     * 查询调拨单数量
     * @return 数量
     */
    public Integer getTodoCountTransfer() {
        LambdaQueryWrapper<Todo> qw = new LambdaQueryWrapper<>();
        qw.eq(Todo::getTodoType, TodoTypeEnum.TRANSFER.getValue());
        qw.eq(Todo::getUserId, SecurityUtils.getUserId());
        qw.eq(Todo::isFinish, false);
        qw.orderByDesc(Todo::getCreateTime);
        return todoMapper.selectCount(qw);
    }

    /**
     * 查询当前用户的任务列表
     *
     * @return 任务列表
     */
    public TodoInfoDTO getTodoInfo() {
        TodoInfoDTO dto = new TodoInfoDTO();
        dto.setAll(getTodoCountAll());
        dto.setApprove(getTodoCountApprove());
        dto.setEmployee(getTodoCountEmployee());
        dto.setTransfer(getTodoCountTransfer());
        return dto;
    }

    /**
     * 查询审批任务数量
     * @return 数量
     */
    public IPage<Todo> getApproveList(Pageable page, boolean finish) {
        LambdaQueryWrapper<Todo> qw = new LambdaQueryWrapper<>();
        qw.eq(Todo::getTodoType, TodoTypeEnum.APPROVE.getValue());
        qw.eq(Todo::getUserId, SecurityUtils.getUserId());
        qw.eq(Todo::isFinish, finish);
        qw.orderByDesc(Todo::getCreateTime);
        return todoMapper.selectPage(new Page<>(page.getPageNumber(), page.getPageSize()), qw);
    }

    /**
     * 查询用户申请列表
     * @return 数量
     */
    public IPage<Todo> getEmployeeList(Pageable page) {
        LambdaQueryWrapper<Todo> qw = new LambdaQueryWrapper<>();
        qw.eq(Todo::getTodoType, TodoTypeEnum.EMPLOYEE.getValue());
        qw.eq(Todo::getUserId, SecurityUtils.getUserId());
        qw.eq(Todo::isFinish, false);
        qw.orderByDesc(Todo::getCreateTime);
        return todoMapper.selectPage(new Page<>(page.getPageNumber(), page.getPageSize()), qw);
    }

    /**
     * 查询用户申请列表
     * @return 数量
     */
    public IPage<Todo> getTransferList(Pageable page) {
        LambdaQueryWrapper<Todo> qw = new LambdaQueryWrapper<>();
        qw.eq(Todo::getTodoType, TodoTypeEnum.TRANSFER.getValue());
        qw.eq(Todo::getUserId, SecurityUtils.getUserId());
        qw.eq(Todo::isFinish, false);
        qw.orderByDesc(Todo::getCreateTime);
        return todoMapper.selectPage(new Page<>(page.getPageNumber(), page.getPageSize()), qw);
    }

    @Transactional
    public void doTodo(Todo todo) {
        String taskId = todo.getTaskId();
        String remark = todo.getRemark();

        // 完成待办任务
        if (todo.getPass() == 1) {
            flowService.completeTask(taskId, remark);
        } else if (todo.getPass() == 2) {
            flowService.rejectTask(taskId, remark);
        }else {
            throw new BusinessException("无效的待办同意类型");
        }

        // 更新待办信息
        LambdaUpdateWrapper<Todo> qw = new LambdaUpdateWrapper<>();
        qw.eq(Todo::getId, todo.getId());
        qw.set(Todo::getPass, todo.getPass());
        qw.set(Todo::isFinish, true);
        qw.set(Todo::getRemark, remark);
        todoMapper.update(null, qw);
    }

    public void createTodo(FlowApply flowApply, Integer todoType) {

        String applyId = flowApply.getId();
        String applyNumber = flowApply.getNumber();

        Todo todo = new Todo();
        todo.setApplyId(applyId);
        todo.setApplyType(flowApply.getApplyType());
        todo.setApplyTypeName(flowApply.getApplyTypeName());
        todo.setApplyNumber(applyNumber);
        todo.setApplyTime(flowApply.getCreateTime());

        // 申请人ID
        Long applyUserId = flowApply.getCreateUserId();
        todo.setApplyUserId(applyUserId);
        SysUserDetailDTO applyUserDetail = sysUserService.getSysUserDetailById(applyUserId);
        todo.setApplyUserName(applyUserDetail.getNickName());
        todo.setApplyCompanyId(applyUserDetail.getCompanyId());
        todo.setApplyDeptId(applyUserDetail.getDeptId());
        Company userCompany = applyUserDetail.getCompany();
        SysDept userDept = applyUserDetail.getDept();
        if (userCompany != null) {
            todo.setApplyCompanyName(userCompany.getName());
        }
        if (userDept != null) {
            todo.setApplyDeptName(userDept.getDeptName());
        }
        todo.setTodoType(todoType);
        todo.setTodoType(todoType);
        todoMapper.insert(todo);
    }

}
